package tests;
import java.util.Arrays;

import tools.Floatmath;
import tools.Help;
import network.NetworkConstants;
import network.dataFormats.NetworkData;
import network.socket.*;
public class TCPSocketTest {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int p = 5432;
		TCPServer s = new TCPServer(p);
		TCPClient c = new TCPClient("127.0.0.1", 5432);
		while (s.getNumSockets() == 0);
		int sent = 0;
		String r = "";
		int sec = 10;
		long t = System.currentTimeMillis();
		int o = 0;
		int recv = 0;
		while (true) {
			byte data[] = new byte[(int) (1+Math.random() * NetworkConstants.PACKAGESIZE)];
			for (int i = 0; i < data.length; i++) {
				data[i] = (byte) ((i + o) % Byte.MAX_VALUE);
			}
			o++;
			s.send(data, 0);
			sent += data.length;
			try {
				Thread.sleep(0);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			byte b[] = c.getReceiveData();
			while (b != null) {
				recv += b.length;
				r = r + new String(b);
				validate(b, data);
				b = c.getReceiveData();
			}
			if (System.currentTimeMillis() - t > 1000 * sec) {
				break;
			}
		}
		Help.p("sent:" + sent / 1024.0 + "kb recv:" + recv / 1024.0 + "kb = " + ((float) recv / (float) sec) / 1024.0f + " kb/s  tested:" + sec + " sec");
		s.close();
		c.close();
	}
	private static void validate(byte[] b, byte[] d) {
		if (b.length != d.length) {
			Help.p("length is not matching!!! b:" + b.length + ",d:" + d.length);
			return;
		}
		for (int i = 0; i < b.length; i++) {
			if (b[i] != d[i]) {
				Help.p("value missmatch at:" + i);
			}
		}
	}
}
